package com.unohacha.qqj.fzdr.login;

import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.unohacha.qqj.fzdr.MainActivity;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.BaseBean;
import com.unohacha.qqj.fzdr.bean.loginbean.LoginBean;
import com.unohacha.qqj.fzdr.imp.MyCallBack;
import com.unohacha.qqj.fzdr.imp.UrlConfig;
import com.unohacha.qqj.fzdr.imp.XUtil;
import com.unohacha.qqj.fzdr.personal.bean.UserInfoBean;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.fzdr.view.ClearEditText;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登录 LOGIN
 */
public class LoginActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.common_right_text)
    TextView commonRightText;
    @BindView(R.id.login_username)
    ClearEditText loginUsername;
    @BindView(R.id.login_password)
    ClearEditText loginPassword;
    @BindView(R.id.login_hide_password)
    CheckBox loginHidePassword;
    @BindView(R.id.login_register_now)
    TextView loginRegisterNow;
    @BindView(R.id.login_forget_pw)
    TextView loginForgetPw;
    @BindView(R.id.login_weixin)
    ImageView loginWeixin;
    @BindView(R.id.login_qq)
    ImageView loginQq;
    @BindView(R.id.loginBut)
    Button loginBut;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        commonBack.setVisibility(View.INVISIBLE);
        commonTitle.setVisibility(View.INVISIBLE);
        commonRightImage.setVisibility(View.INVISIBLE);
        loginHidePassword.setOnCheckedChangeListener(this);
        if (!TextUtils.isEmpty(Prefer.getInstance().getToken())) {
            jumpToOtherActivity(LoginActivity.this, MainActivity.class);
            finish();
        }
    }


    @OnClick({R.id.login_register_now, R.id.login_forget_pw,
            R.id.login_weixin, R.id.login_qq, R.id.loginBut})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.login_register_now://注册
                jumpToOtherActivity(this, RegisterActivity.class);
                break;
            case R.id.login_forget_pw://忘记密码
                jumpToOtherActivity(this, SecureActivity.class);
                break;
            case R.id.loginBut://登录
                if (checkedPut()) {
                    loginFun();
                }
                break;
            case R.id.login_weixin://微信登录
                break;
            case R.id.login_qq://QQ 登录
                break;
        }
    }

    /**
     * 检测参数
     */
    private boolean checkedPut() {
        if (TextUtils.isEmpty(loginUsername.getText().toString().trim())) {
            MyToast(getResources().getString(R.string.text_user_phone));
            return false;
        }
        if (TextUtils.isEmpty(loginPassword.getText().toString().trim())) {
            MyToast(getResources().getString(R.string.text_user_pwd));
            return false;
        }
        return true;
    }

    /**
     * 登录
     */
    private void loginFun() {
        Map<String, Object> params = new HashMap<>();
        params.put("telephone", loginUsername.getText().toString().trim());

        params.put("password", loginPassword.getText().toString().trim());
        params.put("member_cid", "");//个推的ClientId
        params.put("phone_type", "2");//1-IOS；2-安卓
        XUtil.Post(UrlConfig.LOGIN, params, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    MyToast(jsonObject.optString("message"));
                    if ("1".equals(jsonObject.optString("code"))) {
                        Gson gson = new Gson();
                        LoginBean loginBean = gson.fromJson(result, LoginBean.class);
                        Prefer.getInstance().setToken(loginBean.getData().getToken());
                        getUserInfo(loginBean.getData().getToken());

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked == true) {
            loginPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else {
            loginPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
        loginPassword.setSelection(loginPassword.getText().toString().length());
    }

    private void getUserInfo(String token){
        SuperHttp.post("api/mycenter/myNews")
                .addParam("token",token)
                .request(new SimpleCallBack<BaseBean<UserInfoBean>>() {
                    @Override
                    public void onSuccess(BaseBean<UserInfoBean> data) {
                        if(data.getCode()==1){
                            Prefer.getInstance().setChatid(data.getData().getRongyun_token());
                            Prefer.getInstance().setName(data.getData().getCompany_name());
                            Prefer.getInstance().setUser_id(data.getData().getId());
                            Prefer.getInstance().setImage(data.getData().getThumb());
                            jumpToOtherActivity(LoginActivity.this, MainActivity.class);
                            finish();
                        }

                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }
}
